﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using HRM.HRMServiceProxy;

namespace HRM
{
    public partial class frmMarkWorkInfo : Form
    {
        MarkWorkClient wcfMarkWork;
        int _MarkWorkID;
        MarkWorkInfo _MarkWorkInfo;
        DialogResult dlgResult;
        EmployeeClient wcfEmployee;
        PositionDetailClient wcfPD;
        ShiftClient wcfShift;
        int _EmployeeID = 0;
        int _ShiftID = 0;
        bool isChange = false;
        public frmMarkWorkInfo(int MarkWorkID)
        {
            _MarkWorkID = MarkWorkID;
            InitializeComponent();
            LoadLanguage();
            dlgResult = DialogResult.Cancel;
            wcfPD = Service.PositionDetail();
            wcfEmployee = Service.Employee();
            wcfShift = Service.Shift();
            wcfMarkWork = Service.MarkWork();
            _MarkWorkInfo = new MarkWorkInfo();
            if (_MarkWorkID > 0)
            {
                _MarkWorkInfo = wcfMarkWork.MarkWork_GetInfo(_MarkWorkID);
                _EmployeeID = _MarkWorkInfo.EmployeeID;
                _ShiftID = _MarkWorkInfo.ShiftID;
                Info2Form();
            }
        }

        void LoadLanguage()
        {
            if(this.Tag != null)
            this.Text = UpdateUI.GetString(this.Tag.ToString());
            UpdateUI.Load_ToolStrip(ref toolStrip1);
            UpdateUI.Load_Panel(ref panel2);
        }
        private void Info2Form() 
        {

            txtDateWork.Text = _MarkWorkInfo.CheckDate.ToString("dd/MM/yyyy");
            txtDepartment.Text = wcfPD.PositionDetail_GetWithEmployee(_EmployeeID).Tables[0].Rows[0]["DepartmentName"].ToString();
            txtEmployee.Text = wcfEmployee.Employee_GetInfo(_EmployeeID).LastName + " " + wcfEmployee.Employee_GetInfo(_EmployeeID).FirstName;
            txtShift.Text = wcfShift.Shift_GetInfo(_ShiftID).Name;
            txtOvertime.Text = _MarkWorkInfo.OverTime.ToString();
            isChange = false;
        }

        private void Form2Info()
        {
            _MarkWorkInfo.CheckDate = DateTime.ParseExact(txtDateWork.Text, "dd/MM/yyyy",null);
            _MarkWorkInfo.OverTime = Convert.ToInt32(txtOvertime.Text);
        }

        private void frmMarkWorkInfo_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Enter:
                    if (!e.Alt && !e.Control)
                    {
                        this.ProcessTabKey(!e.Shift);
                        e.Handled = true;
                    }
                    break;
                case Keys.Escape:
                    this.Dispose();
                    this.Close();
                    break;
                case Keys.F3:
                    btnSave_Click(null, null);
                    break;
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            wcfMarkWork = Service.MarkWork();
            Form2Info();
            if (_MarkWorkID > 0)//update
            {
                if (wcfMarkWork.MarkWork_UpdateOverTime(_MarkWorkInfo))
                {
                    Program.taskbarNotifier.Show(UpdateUI.GetString("thongbao"), UpdateUI.GetString("capnhatthanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                    isChange = false;
                    dlgResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show(UpdateUI.GetString("capnhatkhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    isChange = true;
                }
            }   
        }

        private void frmMarkWorkInfo_Activated(object sender, EventArgs e)
        {
            LoadLanguage();
        }

        private void DataChange(object sender, EventArgs e)
        {
            isChange = true;
        }

        private void frmMarkWorkInfo_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (isChange)
            {
                if (MessageBox.Show(UpdateUI.GetString("bandathaydoidulieu") + "\n" + UpdateUI.GetString("coluutruockhithoatkhong"), UIParamater.TitleProject, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    btnSave_Click(null, null);
                    if (isChange)
                        e.Cancel = true;
                    else
                        e.Cancel = false;
                }
            }
            this.DialogResult = dlgResult;
        }
    }
}
